import systemdemo, {
    Num2ColumnChar,
    fill_cell,
    get_cell_value,
    getLastRowIndexOfSheet, set_cells
} from '@/components/ts/systemdemo'
import {storeToRefs} from 'pinia'
import {useCCPPStore, LeakageParaName} from '@/store'
import {computed, isRef} from 'vue'
import lodash from 'lodash'
import {df_from_excel} from '@/components/ts/EtPandas'
import {getLeakagePointEx, getPointNameEx, hasValue, make_equal} from '@/components/ts/enum'

const store = useCCPPStore()

const {
    sp,
    leakage_paras,
    leakageRegion,
    hasTCA
} = storeToRefs(store)
const printToTerm = store.printToTerm

const leakage_para_names = computed(() => leakage_paras.value.map(para => para.name))
const hasLeakage = (name: LeakageParaName) => {
    return lodash.includes(leakage_para_names.value, name)
}

export const set_leakage_title = (sheet_name = '漏汽') => {
    systemdemo.clear_sheet(sheet_name)
    systemdemo.create_new_sheet(sheet_name)

    if (hasLeakage('高压门杆')) {

    }
    if (hasLeakage('高压前轴封')) {
        const _ = leakage_paras.value.find(para => para.name === '高压前轴封')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 2
        printToTerm(`高压缸前轴封漏汽`)
        const data = [
            [``, '主蒸汽流量', 't/h'],
            [``, `高压缸前轴封一漏至${_.一漏至}流量`, 't/h'],
            ...[
                _.二漏至 && _.二漏至 !== '无' ? [``, `高压缸前轴封二漏至${_.二漏至}流量`, 't/h'] : []
            ],
            ...[
                _.三漏至 && _.三漏至 !== '无' ? [``, `高压缸前轴封三漏至${_.三漏至}流量`, 't/h'] : []
            ],
            ...[
                _.四漏至 && _.四漏至 !== '无' ? [``, `高压缸前轴封四漏至${_.四漏至}流量`, 't/h'] : []
            ]
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
    if (hasLeakage('高压后轴封')) {
        const _ = leakage_paras.value.find(para => para.name === '高压后轴封')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 3
        const data = [
            [``, `高压缸后轴封一漏至${_.一漏至}流量`, 't/h'],
            ...[
                _.二漏至 && _.二漏至 !== '无' ? [``, `高压缸后轴封二漏至${_.二漏至}流量`, 't/h'] : []
            ],
            ...[
                _.三漏至 && _.三漏至 !== '无' ? [``, `高压缸后轴封三漏至${_.三漏至}流量`, 't/h'] : []
            ],
            ['', '', ''],
            ...[
                _.四漏至 && _.四漏至 !== '无' ? [``, `高压缸后轴封四漏至${_.四漏至}流量`, 't/h'] : []
            ]
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
    if (hasLeakage('中压前轴封')) {

    }
    if (hasLeakage('中压后轴封')) {
        const _ = leakage_paras.value.find(para => para.name === '中压后轴封')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 3
        const data = [
            [``, `中压缸后轴封一漏至${_.一漏至}流量`, 't/h'],
            ...[
                _.二漏至 && _.二漏至 !== '无' ? [``, `中压缸后轴封二漏至${_.二漏至}流量`, 't/h'] : []
            ],
            ...[
                _.三漏至 && _.三漏至 !== '无' ? [``, `中压缸后轴封三漏至${_.三漏至}流量`, 't/h'] : []
            ],
            ...[
                _.四漏至 && _.四漏至 !== '无' ? [``, `中压缸后轴封四漏至${_.四漏至}流量`, 't/h'] : []
            ]
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
    if (hasLeakage('低压轴封供汽')) {
        const _ = leakage_paras.value.find(para => para.name === '低压轴封供汽')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 3
        const data = [
            [``, '低压缸轴封供汽供汽流量', 't/h']
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
    if (hasLeakage('低压缸轴封漏汽')) {
        const _ = leakage_paras.value.find(para => para.name === '低压缸轴封漏汽')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 3
        const data = [
            [``, '低压缸轴封漏汽漏汽流量', 't/h']
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
    if (hasLeakage('SSR溢流')) {
        const _ = leakage_paras.value.find(para => para.name === 'SSR溢流')
        const start_row = getLastRowIndexOfSheet(sheet_name) + 3
        const data = [
            [``, 'SSR溢流溢流流量', 't/h']
        ]
        systemdemo.set_cells(start_row, 1, data, sheet_name)
    }
}

export const load_leakage_sheet = (sheet_name = '漏汽'): {
    yVar: string,
    x: { xVar: string, row: number, startCol: number, endCol: number, region: string },
    y: { yVar: string, row: number, startCol: number, endCol: number, region: string },
    curveRegion: string
}[] => {
    if (systemdemo.exist_sheet(sheet_name)) {
        let df = df_from_excel(sheet_name, undefined, undefined, true, false)
        const lastCol = systemdemo.getLastColumnIndexOfSheet(sheet_name)
        let leakageRegion1 = []
        let x = null

        for (let i = 1; i <= systemdemo.getLastRowIndexOfSheet(sheet_name); i++) {
            const rowTitle = get_cell_value(i, 2, sheet_name)
            let y = null
            if (lodash.includes(rowTitle, '主蒸汽流量')) {
                x = {
                    xVar: rowTitle,
                    row: i,
                    startCol: 4,
                    endCol: lastCol,
                    region: `${sheet_name}!B${i}:${sheet_name}!${Num2ColumnChar(lastCol)}${i}`
                }
            } else if (rowTitle.startsWith('高压缸') || rowTitle.startsWith('中压缸') || rowTitle.startsWith('低压缸') || rowTitle.startsWith('SSR')) {
                y = {
                    yVar: rowTitle, row: i, startCol: 4, endCol: lastCol,
                    region: `${sheet_name}!B${i}:${sheet_name}!${Num2ColumnChar(lastCol)}${i}`
                }
                leakageRegion1.push({yVar: rowTitle, x, y, curveRegion: `${x!.region},${y.region}`})
            }
        }
        // console.log(leakageRegion1)
        leakageRegion.value = leakageRegion1
        return leakageRegion1
    }
}

/**
 * 计算轴封漏汽
 */
const calLeakage = (sheet_name = '汽机') => {
    printToTerm('计算轴封漏汽...')

    function set_leakage_title() {
        if (hasLeakage('高压门杆')) {

        }
        if (hasLeakage('高压前轴封')) {
            const _ = leakage_paras.value.find(para => para.name === '高压前轴封')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            printToTerm(`高压缸前轴封漏汽`)
            const data = [
                [`高压缸前轴封`, '漏汽总流量', 't/h'],
                [``, `一漏至${_.一漏至}流量`, 't/h'],
                ['', `一漏至${_.一漏至}焓`, 'kJ/kg'],
                ['', `一漏至${_.一漏至}能量`, 'MJ/h'],
                ['', '', ''],
                ...[
                    _.二漏至 && _.二漏至 !== '无' ? [``, `二漏至${_.二漏至}流量`, 't/h'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}焓`, 'kJ/kg'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.三漏至 && _.三漏至 !== '无' ? [``, `三漏至${_.三漏至}流量`, 't/h'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}焓`, 'kJ/kg'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.四漏至 && _.四漏至 !== '无' ? [``, `四漏至${_.四漏至}流量`, 't/h'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}焓`, 'kJ/kg'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}能量`, 'MJ/h'] : []
                ]
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
        if (hasLeakage('高压后轴封')) {
            const _ = leakage_paras.value.find(para => para.name === '高压后轴封')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            const data = [
                [`高压缸后轴封`, '漏汽总流量', 't/h'],
                [``, `一漏至${_.一漏至}流量`, 't/h'],
                ['', `一漏至${_.一漏至}焓`, 'kJ/kg'],
                ['', `一漏至${_.一漏至}能量`, 'MJ/h'],
                ['', '', ''],
                ...[
                    _.二漏至 && _.二漏至 !== '无' ? [``, `二漏至${_.二漏至}流量`, 't/h'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}焓`, 'kJ/kg'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.三漏至 && _.三漏至 !== '无' ? [``, `三漏至${_.三漏至}流量`, 't/h'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}焓`, 'kJ/kg'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.四漏至 && _.四漏至 !== '无' ? [``, `四漏至${_.四漏至}流量`, 't/h'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}焓`, 'kJ/kg'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', '']
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
        if (hasLeakage('中压前轴封')) {

        }
        if (hasLeakage('中压后轴封')) {
            const _ = leakage_paras.value.find(para => para.name === '中压后轴封')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            const data = [
                [`中压缸后轴封`, '漏汽总流量', 't/h'],
                [``, `一漏至${_.一漏至}流量`, 't/h'],
                ['', `一漏至${_.一漏至}焓`, 'kJ/kg'],
                ['', `一漏至${_.一漏至}能量`, 'MJ/h'],
                ['', '', ''],
                ...[
                    _.二漏至 && _.二漏至 !== '无' ? [``, `二漏至${_.二漏至}流量`, 't/h'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}焓`, 'kJ/kg'] : [],
                    _.二漏至 && _.二漏至 !== '无' ? ['', `二漏至${_.二漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.三漏至 && _.三漏至 !== '无' ? [``, `三漏至${_.三漏至}流量`, 't/h'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}焓`, 'kJ/kg'] : [],
                    _.三漏至 && _.三漏至 !== '无' ? ['', `三漏至${_.三漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', ''],
                ...[
                    _.四漏至 && _.四漏至 !== '无' ? [``, `四漏至${_.四漏至}流量`, 't/h'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}焓`, 'kJ/kg'] : [],
                    _.四漏至 && _.四漏至 !== '无' ? ['', `四漏至${_.四漏至}能量`, 'MJ/h'] : []
                ],
                ['', '', '']
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
        if (hasLeakage('低压轴封供汽')) {
            const _ = leakage_paras.value.find(para => para.name === '低压轴封供汽')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            const data = [
                [`低压缸轴封供汽`, '供汽流量', 't/h'],
                ['', '供汽焓', 'kJ/kg'],
                ['', '供汽能量', 'MJ/h'],
                ['', '', '']
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
        if (hasLeakage('低压缸轴封漏汽')) {
            const _ = leakage_paras.value.find(para => para.name === '低压缸轴封漏汽')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            const data = [
                [`低压缸轴封漏汽`, '漏汽流量', 't/h'],
                ['', '漏汽焓', 'kJ/kg'],
                ['', '漏汽能量', 'MJ/h'],
                ['', '', '']
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
        if (hasLeakage('SSR溢流')) {
            const _ = leakage_paras.value.find(para => para.name === 'SSR溢流')
            const start_row = getLastRowIndexOfSheet(sheet_name) + 2
            const data = [
                [`SSR溢流`, '流入SSR的总流量', 't/h'],
                ['', 'SSR至低压缸轴封供汽流量', 't/h'],
                ['', 'SSR溢流至凝汽器流量', 't/h'],
                ['', '溢流焓', 'kJ/kg'],
                ['', '溢流能量', 'MJ/h'],
                ['', '', '']
            ]
            systemdemo.set_cells(start_row, 1, data, sheet_name)
        }
    }

    set_leakage_title() // 设置漏汽区域的计算标题

    // -------------------------------- 最后再填充漏汽信息，因为漏汽信息依赖高压主汽流量 ------------------------------
    // -------------------------------- 此处只记录漏气位置，不填写漏气计算结果 ----------------------------------
    let section = '' // 填充数据区域的标题，即A列的标注

    for (let i = 5; i <= getLastRowIndexOfSheet(sheet_name); i++) {
        const _ = (get_cell_value(i, 1, sheet_name) as string).trim()
        if (_ !== '') {
            section = _
        }
        for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
            const var_name = get_cell_value(i, 2, sheet_name) // 当前行对应的参数名称
            if (var_name && var_name !== '') {
                fill_cell(i, j, 'formula', `{leakage.${section}${var_name}}=0`, 'delete', sp)
            }
        }
    }

    const leakageRegionLastRow = getLastRowIndexOfSheet(sheet_name)
    systemdemo.goto(sheet_name, leakageRegionLastRow - 3, 1)
    printToTerm('漏汽计算项设置完毕')

    // 判断是否存在FGH进水流量或者回水流量
    let fgh_flow = 'fgh_water_in.F'
    if (!hasValue(sp as any, 'fgh_water_in.F')) {
        if (hasValue(sp as any, 'fgh_water_out.F')) {
            fgh_flow = 'fgh_water_out.F'
        }
    }

    let i = leakageRegionLastRow + 2
    let data = [
        [`不明泄漏量计算`, `凝汽器当量流量`, 't/h'],
        [``, `高压汽包当量流量`, 't/h'],
        [``, `中压汽包当量流量`, 't/h'],
        [``, `低压汽包当量流量`, 't/h'],
        [``, `明漏量`, 't/h'],
        [``, `不明漏量`, 't/h'],
        [``, `高压部分泄露量`, 't/h'],
        [``, `中压部分泄露量`, 't/h'],
        [``, `低压部分泄露量`, 't/h'],
        [``, `不明泄露率`, '%'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'para', 'condenser.F_lvl', 'delete', sp)
        fill_cell(i + 1, j, 'para', 'drum_high.F_lvl', 'delete', sp)
        fill_cell(i + 2, j, 'para', 'drum_mid.F_lvl', 'delete', sp)
        fill_cell(i + 3, j, 'para', 'drum_low.F_lvl', 'delete', sp)
        fill_cell(i + 4, j, 'para', 'leakage_known.F', 'delete', sp)
        fill_cell(i + 5, j, 'formula', `{leakage_unknown.F}={row-5}+{row-4}+{row-3}+{row-2}-{row-1}`, 'delete', sp)
        fill_cell(i + 6, j, 'formula', '{drum_high.leakage}={row-1}/3', 'delete', sp)
        fill_cell(i + 7, j, 'formula', '{drum_mid.leakage}={row-2}/3', 'delete', sp)
        fill_cell(i + 8, j, 'formula', '{drum_low.leakage}={row-3}/3', 'delete', sp)
        let form = `{leakage_unknown.rate}={row-4}/({water_condense_pump_out.F}+{${fgh_flow}})*100`
        // console.log(form)
        fill_cell(i + 9, j, 'formula', form, 'delete', sp)
    }

    // -------------------------------- 高压至高排部分计算----------------------------------
    i = getLastRowIndexOfSheet(sheet_name) + 2
    data = [
        [`汽轮机本体计算`, `余热锅炉高压给水压力`, 'MPa'],
        [``, `余热锅炉高压给水温度`, '℃'],
        ['', '余热锅炉高压给水焓值', 'kJ/kg'],
        ...[
            hasTCA.value ? [``,
                store.hpConfig.includesDesuperheater ? `高压给水流量（含减温水不含TCA）` : '高压给水流量（不含减温水和TCA）',
                't/h'
            ] : ['',
                store.hpConfig.includesDesuperheater ? `高压给水流量（含减温水）` : '高压给水流量（不含减温水）',
                't/h'
            ]
        ],
        ...[
            hasTCA.value ? [``, `高压给水至TCA流量`, 't/h'] : []
        ],
        ['', '高压过热器减温水流量', 't/h'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'para', 'feed_water_high.P', 'delete', sp)
        fill_cell(i + 1, j, 'para', 'feed_water_high.T', 'delete', sp)
        fill_cell(i + 2, j, 'formula', '{feed_water_high.H}=if97.H_PT({row-2},{row-1})', 'delete', sp)
        if (hasTCA.value) {

        } else {
            fill_cell(i + 3, j, 'para', 'feed_water_high.F', 'delete', sp)
            fill_cell(i + 5, j, 'para', 'water_overheat_reducing.F', 'delete', sp)
        }
    }

    i = getLastRowIndexOfSheet(sheet_name) + 2
    data = [
        [``, `高压主蒸汽压力`, 'MPa'],
        [``, `高压主蒸汽温度`, '℃'],
        ['', '高压主蒸汽焓值', 'kJ/kg'],
        ['', '高压主蒸汽熵值', 'kJ/kgK'],
        ['', '高压主蒸汽流量', 't/h'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'para', 'main_steam_high.P', 'delete', sp)
        fill_cell(i + 1, j, 'para', 'main_steam_high.T', 'delete', sp)
        fill_cell(i + 2, j, 'formula', '{main_steam_high.H}=if97.H_PT({row-2},{row-1})', 'delete', sp)
        fill_cell(i + 3, j, 'formula', '{main_steam_high.S}=if97.S_PT({row-3},{row-2})', 'delete', sp)
        systemdemo.set_cell_format(i + 3, j, '0.000', sheet_name)
        // console.log('包含减温水？：', store.hpConfig.includesDesuperheater)
        if (store.hpConfig.includesDesuperheater) {
            if (store.hpConfig.includesTCA) {
                fill_cell(i + 4, j, 'formula', '{main_steam_high.F}={feed_water_high.F}-{tca_water_in.F}-{drum_high.leakage}', 'delete', sp, 'last')
            } else {
                fill_cell(i + 4, j, 'formula', '{main_steam_high.F}={feed_water_high.F}-{drum_high.leakage}', 'delete', sp, 'last')
            }
        } else {
            if (store.hpConfig.includesTCA) {
                fill_cell(i + 4, j, 'formula', '{main_steam_high.F}={feed_water_high.F}+{water_overheat_reducing.F}-{tca_water_in.F}-{drum_high.leakage}', 'delete', sp, 'last')
            } else {
                fill_cell(i + 4, j, 'formula', '{main_steam_high.F}={feed_water_high.F}+{water_overheat_reducing.F}-{drum_high.leakage}', 'delete', sp, 'last')
            }
        }
    }

    i = getLastRowIndexOfSheet(sheet_name) + 2
    data = [
        [``, `高压缸排汽压力`, 'MPa'],
        [``, `高压缸排汽温度`, '℃'],
        ['', '高压缸排汽焓值', 'kJ/kg'],
        ['', '高压缸排汽流量', 't/h'],
        ['', '高压缸排汽等熵焓', 'kJ/kg'],
        ['', '高压缸有效焓降', 'kJ/kg'],
        ['', '高压缸等熵焓降', 'kJ/kg'],
        ['', '高压缸效率', '%'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    // console.log(sp.value)
    // 获取高压前轴封漏气总流量
    const t1 = getLeakagePointEx(sp as any, 'leakage', '高压缸前轴封漏汽总流量')[0]?.value
    const to_高排: [{ key: string, value: string }] = getLeakagePointEx(sp as any, 'leakage', '', '高排流量') as any
    const t2 = getLeakagePointEx(sp as any, 'leakage', '高压缸后轴封漏汽总流量')[0]?.value
    let totalFString = t1 ? `-${t1}-${t2}` : `-${t2}` // 高中压合缸时，可能没有高压缸前轴封漏汽，或前轴封漏汽流量比较难计算
    for (let item of to_高排) {
        totalFString += `+${item.value}`
    }

    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        const newTotalFString = systemdemo.hrefStr2ActualStr(totalFString, j)

        fill_cell(i, j, 'para', 'outlet_high.P', 'delete', sp)
        fill_cell(i + 1, j, 'para', 'outlet_high.T', 'delete', sp)
        fill_cell(i + 2, j, 'formula', '{outlet_high.H}=if97.H_PT({row-2},{row-1})', 'delete', sp)
        fill_cell(i + 3, j, 'formula', `{outlet_high.F}={main_steam_high.F}${newTotalFString}`, 'delete', sp)
        fill_cell(i + 4, j, 'formula', `{outlet_high.H_等熵}=if97.H_PS({row-4},{main_steam_high.S})`, 'delete', sp)
        fill_cell(i + 5, j, 'formula', `{outlet_high.dH_inlet_high}={main_steam_high.H}-{row-3}`, 'delete', sp)
        fill_cell(i + 6, j, 'formula', `{outlet_high.dH_等熵_inlet_high}={main_steam_high.H}-{row-2}`, 'delete', sp)
        fill_cell(i + 7, j, 'formula', `{turbine_high.eta}={row-2}/{row-1}*100`, 'delete', sp)
    }
    // -------------------------------- 高压至高排部分计算----------------------------------


    // ----------------------------------- 中压给水至再热部分计算 -------------------------------------------
    i = getLastRowIndexOfSheet(sheet_name) + 2
    systemdemo.goto(sheet_name, i - 3, 1)
    data = [
        [``, `余热锅炉中压给水压力`, 'MPa'],
        [``, `余热锅炉中压给水温度`, '℃'],
        ['', '余热锅炉中压给水焓', 'kJ/kg'],
        ['', '中压给水流量（不含FGH和减温水）', 't/h'],
        ['', '中压给水至FGH流量', 't/h'], // i+4
        ['', '再热器减温水流量', 't/h'], // i+5

        ['', '中压蒸汽流量（高排混合前）', 't/h'], // i+6
        ['', '', ''],
        ['', '中压缸进汽压力', 'MPa'], // i+8
        ['', '中压缸进汽温度', '℃'],
        ['', '中压缸进汽焓', 'kJ/kg'],
        ['', '中压缸进汽熵', 'kJ/(kgK)'],
        ['', '中压缸进汽流量', 't/h'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    // console.log(sp.value)
    // 获取高压前轴封漏气总流量

    const item2 = sp.value['fgh_water_out']
    console.log()
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'para', 'feed_water_mid.P', 'delete', sp)
        fill_cell(i + 1, j, 'para', 'feed_water_mid.T', 'delete', sp)
        fill_cell(i + 2, j, 'formula', '{feed_water_mid.H}=if97.H_PT({row-2},{row-1})', 'delete', sp)
        fill_cell(i + 3, j, 'para', `feed_water_mid.F`, 'delete', sp)
        if (fgh_flow === 'fgh_water_out.F') {
            fill_cell(i + 4, j, 'formula', `{fgh_water_in.F}={fgh_water_out.F}`, 'delete', sp)
        } else {
            fill_cell(i + 4, j, 'formula', `{fgh_water_out.F}={fgh_water_in.F}`, 'delete', sp)
        }
        fill_cell(i + 5, j, 'para', `water_reheater_reducing.F`, 'delete', sp)
        if (store.mpConfig.includesFGH) {
            if (store.mpConfig.includesDesuperheater) {
                // 包含FGH和减温水
                fill_cell(i + 6, j, 'formula', `{main_steam_mid.F}={row-3}-{row-2}-{row-1}-{drum_mid.leakage}`, 'delete', sp)
            } else {
                // 包含FGH、不包含减温水
                fill_cell(i + 6, j, 'formula', `{main_steam_mid.F}={row-3}-{row-2}-{drum_mid.leakage}`, 'delete', sp)
            }
        } else {
            if (store.mpConfig.includesDesuperheater) {
                // 不包含FGH，包含减温水
                fill_cell(i + 6, j, 'formula', `{main_steam_mid.F}={row-3}-{row-1}-{drum_mid.leakage}`, 'delete', sp)
            } else {
                // 不包含FGH、不包含减温水
                fill_cell(i + 6, j, 'formula', `{main_steam_mid.F}={row-3}-{drum_mid.leakage}`, 'delete', sp)
            }
        }

        fill_cell(i + 8, j, 'para', `inlet_medium.P`, 'delete', sp)
        fill_cell(i + 9, j, 'para', `inlet_medium.T`, 'delete', sp)
        fill_cell(i + 10, j, 'formula', `{inlet_medium.H}=if97.H_PT({row-2},{row-1})`, 'delete', sp)
        fill_cell(i + 11, j, 'formula', `{inlet_medium.S}=if97.S_PT({row-3},{row-2})`, 'delete', sp)
        const formula = `{inlet_medium.F}={row-6}+{outlet_high.F}+{row-7}`
        fill_cell(i + 12, j, 'formula', formula, 'delete', sp)
    }
    // ----------------------------------- 中压给水至再热部分计算 -------------------------------------------

    // ----------------------------------- 再热至中排计算 --------------------------------------
    i = getLastRowIndexOfSheet(sheet_name) + 2
    systemdemo.goto(sheet_name, i - 3, 1)
    data = [
        [``, `再热蒸汽压损`, '%'],
        [``, `中压缸排汽流量`, 't/h'],
        ['', '', ''],
        ['', '中压缸排汽压力', 'MPa'],
        ['', '中压缸排汽温度', '℃'], // i+4
        ['', '中压缸排汽焓', 'kJ/kg'], // i+5
        ['', '中压缸排汽等熵焓', 'kJ/kg'], // i+6
        ['', '中压缸实际焓降', 'kJ/kg'],
        ['', '中压缸等熵焓降', 'kJ/kg'], // i+8
        ['', '中压缸效率', '%'],
        ['', '', '']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    const lm1 = getLeakagePointEx(sp as any, 'leakage', '中压缸前轴封漏汽总流量')[0]?.value
    const to_中排: [{ key: string, value: string }] = getLeakagePointEx(sp as any, 'leakage', '', '中排流量') as any
    const lm2 = getLeakagePointEx(sp as any, 'leakage', '中压缸后轴封漏汽总流量')[0]?.value
    totalFString = lm1 ? `-${lm1}-${lm2}` : `-${lm2}` // 有些小机组中低压合缸，则没有中压缸前轴封漏汽，认为中压前轴封漏汽至低压缸进汽流量为0，这类机组比高中压合缸的机组少
    for (let item of to_中排) {
        totalFString += `+${item.value}`
    }
    // console.log('totalFString = ', totalFString)
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'formula', `{reheater.压损}=({outlet_high.P}-{inlet_medium.P})/{outlet_high.P}*100`, 'delete', sp, 'last')
        fill_cell(i + 1, j, 'formula', `{outlet_medium.F}={inlet_medium.F}${totalFString}`, 'delete', sp)
        fill_cell(i + 3, j, 'para', `outlet_medium.P`, undefined, sp)
        fill_cell(i + 4, j, 'para', `outlet_medium.T`, undefined, sp)
        fill_cell(i + 5, j, 'formula', `{outlet_medium.H}=if97.H_PT({row-2},{row-1})`, 'delete', sp)
        fill_cell(i + 6, j, 'formula', `{outlet_medium.H_等熵}=if97.H_PS({row-3},{inlet_medium.S})`, 'delete', sp, 'last')
        fill_cell(i + 7, j, 'formula', `{outlet_medium.dH_inlet_medium}={inlet_medium.H}-{row-2}`, 'delete', sp)
        fill_cell(i + 8, j, 'formula', `{outlet_medium.dH_等熵_inlet_medium}={inlet_medium.H}-{row-2}`, 'delete', sp)
        fill_cell(i + 9, j, 'formula', `{turbine_medium.eta}={row-2}/{row-1}*100`, 'delete', sp)
    }
    // ----------------------------------- 再热至中排计算 --------------------------------------

    // ----------------------------------- 低压部分计算 --------------------------------------
    i = getLastRowIndexOfSheet(sheet_name) + 2
    systemdemo.goto(sheet_name, i - 3, 1)
    data = [
        [``, `凝结水流量(FGH回水前)`, 't/h'],
        [``, `凝结水流量(FGH回水后)`, 't/h'],
        ['', '低压给水流量(扣除再循环流量)', 't/h'],
        ['', '', ''],
        ['', '余热锅炉低压蒸汽压力', 'MPa'],
        ['', '余热锅炉低压蒸汽温度', '℃'], // i+5
        ['', '余热锅炉低压蒸汽焓', 'kJ/kg'], // i+6
        [``, `余热锅炉低压蒸汽流量`, 't/h'],
        [``, `余热锅炉低压蒸汽能量`, 'MJ/h'],
        ['', '', ''],
        ['', '低压缸进汽压力', 'MPa'], // i+10
        ['', '低压缸进汽温度', '℃'],
        ['', '低压缸进汽焓', 'kJ/kg'], // i+12
        ['', '低压缸进汽流量', 't/h'],
        ['', '低压缸进汽能量', 'MJ/h'],
        ['', '', ''],
        ['', '推算的中排焓', 'kJ/kg'], // i+16
        ['', '', ''],
        ['', '低压缸排汽流量', 't/h'], // i+18
        ['', '', ''],
        ['', '热井出水温度', '℃'], // i+20
        ['', '', ''],
        ['', '凝泵出口母管压力', 'MPa'], // i+22
        ['', '凝泵出口母管温度', '℃'],
        ['', '凝泵出口母管焓', 'kJ/kg'],
        ['', '凝泵出口母管流量', 't/h'],
        ['', '', ''],
        ['', '轴加进汽流量', 't/h'], // i+27
        ['', '轴加进汽焓', 'kJ/kg'],
        ['', '轴加进汽能量', 'MJ'],
        ['', '轴加疏水焓', 'kJ/kg'],
        ['', '轴加疏水流量', 't/h'],
        ['', '轴加出口凝结水温度', '℃'],
        ['', '轴加出口凝结水焓', 'kJ/kg']

    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    let form = `-{feed_water_high.F}-{feed_water_mid.F}`
    if (!store.hpConfig.includesDesuperheater) { // 如果高压给水不包含减温水，则还要减去高压蒸汽减温水流量
        form = `${form}-{water_overheat_reducing.F}`
    }
    if (store.hasTCA && !store.hpConfig.includesTCA) {
        form = `${form}-{tca_water_out.F}`
    }
    if (store.hasFGH && !store.mpConfig.includesFGH) {
        form = `${form}-{fgh_water_in.F}`
    }
    if (!store.mpConfig.includesDesuperheater) {
        form = `${form}-{water_reheater_reducing.F}`
    }
    let leakage_low = ''
    if (store.getLeakageRegionItem('低压缸轴封供汽供汽流量')) {
        leakage_low = `+{leakage.低压缸轴封供汽供汽流量}`
    }
    if (store.getLeakageRegionItem('低压缸轴封漏汽漏汽流量')) {
        leakage_low = `${leakage_low}-{leakage.低压缸轴封漏汽漏汽流量}`
    }

    // water_condense_pump_out凝泵出口凝结水温度可能没有测点，如果没有值则取热井出水温度
    make_equal('water_condense_pump_out', 'heat_well_out', sp.value as any)
    const to_轴加: [{
        key: string,
        value: string
    }] = getLeakagePointEx(sp as any, 'leakage', '', '轴封加热器流量') as any
    const to_轴加能量: [{
        key: string,
        value: string
    }] = getLeakagePointEx(sp as any, 'leakage', '', '轴封加热器能量') as any

    totalFString = ''
    for (let item of to_轴加) {
        totalFString += `+${item.value}`
    }
    totalFString = totalFString.substring(1) // 去掉第一个+
    let totalFString1 = ''
    for (let item of to_轴加能量) {
        totalFString1 += `+${item.value}`
    }
    totalFString1 = totalFString1.substring(1) // 去掉第一个+
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        if (store.condensateConfig.includesFGH) { // 如果包含FGH流量，则需要将凝结水流量对应到低省进口流量上
            fill_cell(i, j, 'formula', `{water_condense_pump_out.F}={economizer_water_inlet.F}-{fgh_water_out.F}`, 'delete', sp)
            fill_cell(i, j, 'para', `economizer_water_inlet.F`, 'delete', sp)
        } else { // 如果不包含FGH流量，则需要将凝结水流量对应到凝泵出口流量上
            fill_cell(i, j, 'para', `water_condense_pump_out.F`, undefined, sp)
            fill_cell(i + 1, j, 'formula', '{economizer_water_inlet.F}={row-1}+{fgh_water_out.F}', undefined, sp, 'last')
        }

        fill_cell(i + 2, j, 'formula', `{feed_water_low.F}={row-1}${form}`, undefined, sp, 'last')
        fill_cell(i + 4, j, 'para', `main_steam_low.P`, 'delete', sp)
        fill_cell(i + 5, j, 'para', `main_steam_low.T`, 'delete', sp)
        fill_cell(i + 6, j, 'formula', `{main_steam_low.H}=if97.H_PT({row-2},{row-1})`, undefined, sp)
        fill_cell(i + 7, j, 'formula', `{main_steam_low.F}={feed_water_low.F}-{drum_low.leakage}`, 'delete', sp, 'last')
        fill_cell(i + 8, j, 'formula', `{main_steam_low.E}={row-2}*{row-1}`, 'delete', sp)

        fill_cell(i + 10, j, 'para', `inlet_low.P`, 'delete', sp)
        fill_cell(i + 11, j, 'para', `inlet_low.T`, 'delete', sp)
        fill_cell(i + 12, j, 'formula', `{inlet_low.H}=if97.H_PT({row-2},{row-1})`, 'delete', sp)
        fill_cell(i + 13, j, 'formula', `{inlet_low.F}={main_steam_low.F}+{outlet_medium.F}`, 'delete', sp, 'last')
        fill_cell(i + 14, j, 'formula', `{inlet_low.E}={row-2}*{row-1}`, 'delete', sp)

        fill_cell(i + 16, j, 'formula', `{outlet_medium.H}=({row-2}-{main_steam_low.E})/{outlet_medium.F}`, 'delete', sp, 'last')

        fill_cell(i + 18, j, 'formula', `{outlet_low.F}={inlet_low.F}${leakage_low}`, 'delete', sp, 'last')

        fill_cell(i + 20, j, 'para', `heat_well_out.T`, 'delete', sp)

        fill_cell(i + 22, j, 'para', `water_condense_pump_out.P`, 'delete', sp)
        fill_cell(i + 23, j, 'para', `water_condense_pump_out.T`, 'delete', sp)
        fill_cell(i + 24, j, 'formula', `{water_condense_pump_out.H}=if97.H_PT({row-2},{row-1})`, 'delete', sp)
        fill_cell(i + 25, j, 'formula', `{water_condense_pump_out.F}={row-7}+{leakage.SSR溢流SSR溢流至凝汽器流量}+{row+2}`, 'delete', sp)

        fill_cell(i + 27, j, 'formula', `{seal_heater_vapor_in.F}=${totalFString}`, 'delete', sp)
        fill_cell(i + 28, j, 'formula', `{seal_heater_vapor_in.H}={row+1}/{row-1}`, 'delete', sp)
        fill_cell(i + 29, j, 'formula', `{seal_heater_vapor_in.E}=${totalFString1}`, 'delete', sp)
        fill_cell(i + 30, j, 'formula', `{seal_heater_vapor_in.E}=${totalFString1}`, 'delete', sp)
        fill_cell(i + 31, j, 'formula', `{seal_heater_vapor_out.F}={row-4}`, 'delete', sp)
        fill_cell(i + 32, j, 'para', `seal_heater_water_out.T`, 'delete', sp)
        fill_cell(i + 33, j, 'formula', `{seal_heater_water_out.H}=if97.H_PT({water_condense_pump_out.P},{row-1})`, 'delete', sp)

    }
    // =================================== 低压部分计算 ======================================

    // ----------------------------------- 更新轴封漏汽信息 ---------------------------------------
    const leakage_h1 = store.getLeakageRegionItem('高压缸前轴封一漏至')
    const leakage_h2 = store.getLeakageRegionItem('高压缸前轴封二漏至')
    const leakage_h3 = store.getLeakageRegionItem('高压缸前轴封三漏至')
    const leakage_h4 = store.getLeakageRegionItem('高压缸前轴封四漏至')
    const leakage_hb1 = store.getLeakageRegionItem('高压缸后轴封一漏至')
    const leakage_hb2 = store.getLeakageRegionItem('高压缸后轴封二漏至')
    const leakage_hb3 = store.getLeakageRegionItem('高压缸后轴封三漏至')
    const leakage_hb4 = store.getLeakageRegionItem('高压缸后轴封四漏至')
    const leakage_m1 = store.getLeakageRegionItem('中压缸前轴封一漏至')
    const leakage_m2 = store.getLeakageRegionItem('中压缸前轴封二漏至')
    const leakage_m3 = store.getLeakageRegionItem('中压缸前轴封三漏至')
    const leakage_m4 = store.getLeakageRegionItem('中压缸前轴封四漏至')
    const leakage_mb1 = store.getLeakageRegionItem('中压缸后轴封一漏至')
    const leakage_mb2 = store.getLeakageRegionItem('中压缸后轴封二漏至')
    const leakage_mb3 = store.getLeakageRegionItem('中压缸后轴封三漏至')
    const leakage_mb4 = store.getLeakageRegionItem('中压缸后轴封四漏至')
    const leakage_l1 = store.getLeakageRegionItem('低压缸轴封供汽供汽流量')
    const leakage_l2 = store.getLeakageRegionItem('低压缸轴封漏汽漏汽流量')

    const to_SSR: [{ key: string, value: string }] = getLeakagePointEx(sp as any, 'leakage', '', 'SSR流量') as any
    // console.log('to_SSR', to_SSR)
    let form_h_ssr: string, form_f_ssr: string
    if (to_SSR && to_SSR.length > 0) {
        // 构建总流量和总能量的计算公式
        let totalFlowFormula = ''
        let totalEnergyFormula = ''
        to_SSR.forEach((item, index) => {
            const flowPoint = item.key
            const HPoint = flowPoint.replace('流量', '焓')
            if (index === 0) {
                totalFlowFormula = `{leakage.${flowPoint}}`
                totalEnergyFormula = `({leakage.${HPoint}}*{leakage.${flowPoint}})`
            } else {
                totalFlowFormula = `${totalFlowFormula}+{leakage.${flowPoint}}`
                totalEnergyFormula = `${totalEnergyFormula}+({leakage.${HPoint}}*{leakage.${flowPoint}})`
            }
        })
        form_f_ssr = `{ssr_inlet.F}=${totalFlowFormula}`
        form_h_ssr = `{ssr_inlet.H}=${totalEnergyFormula}/(${totalFlowFormula})`
    }
    // console.log(`form_f_ssr, form_h_ssr: `, form_f_ssr, form_h_ssr)
    section = '' // 填充数据区域的标题，即A列的标注
    for (let i = 5; i <= leakageRegionLastRow; i++) {
        const _ = (get_cell_value(i, 1, sheet_name) as string).trim()
        if (_ !== '') {
            section = _
        }

        for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
            const var_name = get_cell_value(i, 2, sheet_name) // 当前行对应的参数名称
            if (section === '高压缸前轴封') {
                if (var_name.endsWith('能量')) {
                    if (var_name.startsWith('一漏至') && var_name.endsWith('能量')) {
                        const form = `{leakage.${section}一漏流量}=yk.xz({main_steam_high.F}, ${leakage_h1.curveRegion})`
                        fill_cell(i - 2, j, 'formula', form, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}一漏焓值}={main_steam_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}一漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('二漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}二漏流量}=yk.xz({main_steam_high.F}, ${leakage_h2.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}二漏焓值}={main_steam_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}二漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('三漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}三漏流量}=yk.xz({main_steam_high.F}, ${leakage_h3.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}三漏焓值}={main_steam_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}三漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('四漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}四漏流量}=yk.xz({main_steam_high.F}, ${leakage_h4.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}四漏焓值}={main_steam_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}四漏能量}={row-1}*{row-2}`, 'delete', sp)
                    }
                }
            } else if (section === '高压缸后轴封') {
                if (var_name.endsWith('能量')) {
                    if (var_name.startsWith('一漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}一漏流量}=yk.xz({main_steam_high.F}, ${leakage_hb1.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}一漏焓值}={outlet_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}一漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('二漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}二漏流量}=yk.xz({main_steam_high.F}, ${leakage_hb2.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}二漏焓值}={outlet_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}二漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('三漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}三漏流量}=yk.xz({main_steam_high.F}, ${leakage_hb3.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}三漏焓值}={outlet_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}三漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('四漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}四漏流量}=yk.xz({main_steam_high.F}, ${leakage_hb4.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}四漏焓值}={outlet_high.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}四漏能量}={row-1}*{row-2}`, 'delete', sp)
                    }
                }
            } else if (section === '中压缸前轴封') {
                if (var_name.endsWith('能量')) {
                    if (var_name.startsWith('一漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}一漏流量}=yk.xz({main_steam_high.F}, ${leakage_m1.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}一漏焓值}={inlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}一漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('二漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}二漏流量}=yk.xz({main_steam_high.F}, ${leakage_m2.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}二漏焓值}={inlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}二漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('三漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}三漏流量}=yk.xz({main_steam_high.F}, ${leakage_m3.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}三漏焓值}={inlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}三漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('四漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}四漏流量}=yk.xz({main_steam_high.F}, ${leakage_m4.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}四漏焓值}={inlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}四漏能量}={row-1}*{row-2}`, 'delete', sp)
                    }
                }
            } else if (section === '中压缸后轴封') {
                if (var_name.endsWith('能量')) {
                    if (var_name.startsWith('一漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}一漏流量}=yk.xz({main_steam_high.F}, ${leakage_mb1.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}一漏焓值}={outlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}一漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('二漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}二漏流量}=yk.xz({main_steam_high.F}, ${leakage_mb2.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}二漏焓值}={outlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}二漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('三漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}三漏流量}=yk.xz({main_steam_high.F}, ${leakage_mb3.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}三漏焓值}={outlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}三漏能量}={row-1}*{row-2}`, 'delete', sp)
                    } else if (var_name.startsWith('四漏至') && var_name.endsWith('能量')) {
                        fill_cell(i - 2, j, 'formula', `{leakage.${section}四漏流量}=yk.xz({main_steam_high.F}, ${leakage_mb4.curveRegion})`, 'delete', sp)
                        fill_cell(i - 1, j, 'formula', `{leakage.${section}四漏焓值}={outlet_medium.H}`, 'delete', sp)
                        fill_cell(i, j, 'formula', `{leakage.${section}四漏能量}={row-1}*{row-2}`, 'delete', sp)
                    }
                }
            } else if (section === '低压缸轴封供汽') {
                if (var_name.endsWith('供汽能量')) {
                    fill_cell(i - 2, j, 'formula', `{leakage.${section}供汽流量}=yk.xz({main_steam_high.F}, ${leakage_l1.curveRegion})`, 'delete', sp)
                    // fill_cell(i - 1, j, 'formula', form_h_ssr, 'delete', sp)
                    // systemdemo.set_position_of_para(`leakage.${section}供汽焓值`, `${sheet_name}!{Y}${i - 1}`, sp)
                    fill_cell(i, j, 'formula', `{leakage.${section}供汽能量}={row-1}*{row-2}`, 'delete', sp)
                }
            } else if (section === '低压缸轴封漏汽') {
                if (var_name.endsWith('漏汽能量')) {
                    fill_cell(i - 2, j, 'formula', `{leakage.${section}漏汽流量}=yk.xz({main_steam_high.F}, ${leakage_l2.curveRegion})`, 'delete', sp)
                    fill_cell(i, j, 'formula', `{leakage.${section}漏汽能量}={row-2}*{row-1}`, 'delete', sp)
                }
            } else if (section === 'SSR溢流') {
                if (var_name.endsWith('溢流能量')) {
                    fill_cell(i - 4, j, 'formula', form_f_ssr, undefined, sp)
                    fill_cell(i - 3, j, 'para', `leakage.低压缸轴封供汽供汽流量`, 'delete', sp)
                    fill_cell(i - 2, j, 'formula', `{ssr_outlet.F_to_condenser}={row-2}-{row-1}`, undefined, sp)
                    fill_cell(i - 1, j, 'formula', form_h_ssr, undefined, sp)
                    fill_cell(i, j, 'formula', `{ssr_outlet.E_to_condenser}={row-2}*{row-1}`, undefined, sp)
                }
            }
        }
    }

    section = '' // 再过一遍漏汽区域，这回填充漏汽总流量行
    for (let i = 5; i <= leakageRegionLastRow; i++) {
        const _ = (get_cell_value(i, 1, sheet_name) as string).trim()
        if (_ !== '') {
            section = _
        }
        for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
            const var_name = get_cell_value(i, 2, sheet_name) // 当前行对应的参数名称
            if (section === '高压缸前轴封' && var_name === '漏汽总流量') {
                let form = `{leakage.高压缸前轴封_漏汽总流量}={leakage.${leakage_h1.yVar}}`
                if (leakage_h2) {
                    form = `${form}+{leakage.${leakage_h2.yVar}}`
                }
                if (leakage_h3) {
                    form = `${form}+{leakage.${leakage_h3.yVar}}`
                }
                if (leakage_h4) {
                    form = `${form}+{leakage.${leakage_h4.yVar}}`
                }
                // console.log('form', form)
                fill_cell(i, j, 'formula', form, undefined, sp)
            } else if (section === '高压缸后轴封' && var_name === '漏汽总流量') {
                let form = `{leakage.高压缸后轴封漏汽总流量}={leakage.${leakage_hb1.yVar}}`
                if (leakage_hb2) {
                    form = `${form}+{leakage.${leakage_hb2.yVar}}`
                }
                if (leakage_hb3) {
                    form = `${form}+{leakage.${leakage_hb3.yVar}}`
                }
                if (leakage_hb4) {
                    form = `${form}+{leakage.${leakage_hb4.yVar}}`
                }
                // console.log('form', form)
                fill_cell(i, j, 'formula', form, undefined, sp)
            } else if (section === '中压缸前轴封' && var_name === '漏汽总流量') {
                let form = `{leakage.中压缸后轴封漏汽总流量}={leakage.${leakage_m1.yVar}}`
                if (leakage_m2) {
                    form = `${form}+{leakage.${leakage_m2.yVar}}`
                }
                if (leakage_m3) {
                    form = `${form}+{leakage.${leakage_m3.yVar}}`
                }
                if (leakage_m4) {
                    form = `${form}+{leakage.${leakage_m4.yVar}}`
                }
                fill_cell(i, j, 'formula', form, undefined, sp)
            } else if (section === '中压缸后轴封' && var_name === '漏汽总流量') {
                let form = `{leakage.中压缸后轴封漏汽总流量}={leakage.${leakage_mb1.yVar}}`
                if (leakage_mb2) {
                    form = `${form}+{leakage.${leakage_mb2.yVar}}`
                }
                if (leakage_mb3) {
                    form = `${form}+{leakage.${leakage_mb3.yVar}}`
                }
                if (leakage_mb4) {
                    form = `${form}+{leakage.${leakage_mb4.yVar}}`
                }
                fill_cell(i, j, 'formula', form, undefined, sp)
            } else if (section === '低压缸轴封供汽') {
                if (var_name.endsWith('供汽能量')) {
                    fill_cell(i - 1, j, 'formula', `{leakage.${section}供汽焓值}={ssr_inlet.H}`, 'delete', sp)
                }
            }
        }
    }
    // systemdemo.goto(sheet_name, leakageRegionLastRow - 3, 1)
    // ----------------------------------- 更新轴封漏汽信息 ---------------------------------------
    // ----------------------------------- 汽轮机轴功率计算 ---------------------------------------
    i = getLastRowIndexOfSheet(sheet_name) + 2
    systemdemo.goto(sheet_name, i - 3, 1)
    data = [
        [``, `汽轮发电机功率`, 'MW'],
        [``, `汽轮发电机励磁功率`, 'MW'],
        [``, `汽轮发电机净功率`, 'MW'],
        ['', '', ''],
        [``, `汽轮发电机效率`, '%'],
        ['', '发电机效率', '%'],
        ['', '汽轮机机械损失', 'kW'],
        ['', '汽轮机输出轴功率', 'kW']
    ]
    set_cells(i, 1, data, sheet_name)
    systemdemo.auto_fit_columns()
    for (let j = 4; j <= systemdemo.getLastColumnIndexOfSheet(sheet_name); j++) {
        fill_cell(i, j, 'formula', `{steam_turbine.P}`, 'delete', sp)
    }
    // ==================================== 汽轮机轴功率计算 ========================================
}

export const calSteamTurbine = () => {
    printToTerm(`开始进行汽轮机计算...`)
    systemdemo.clear_sheet('汽机')
    systemdemo.create_new_sheet('汽机')
    if (!store.dataScanned) {
        store.scanData()
        load_leakage_sheet('漏汽')
    }
    systemdemo.add_title_region('汽机')
    systemdemo.auto_fit_columns()
    calLeakage('汽机')
}